<template>
  <el-form
    :model="form"
    status-icon
    :rules="rules"
    ref="form"
    label-width="100px"
    class="login-container"
  >
    <h3 class="login_title">用户注册</h3>
    <el-form-item label="用户名" label-width="80px" prop="username" class="username">
      <el-input
        type="input"
        clearable
        v-model.trim="form.username"
        autocomplete="off"
        placeholder="请输入账号"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" label-width="80px" prop="password">
      <el-input
        type="password"
        clearable
        v-model="form.password"
        autocomplete="off"
        placeholder="请输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" label-width="80px" prop="password2">
      <el-input
        type="password"
        clearable
        v-model="form.password2"
        autocomplete="off"
        placeholder="请再次输入密码"
      ></el-input>
    </el-form-item>
    <el-form-item label="昵称" label-width="80px" prop="nick" class="nick">
      <el-input
        type="input"
        clearable
        v-model.trim="form.nick"
        autocomplete="off"
        placeholder="请输入昵称"
      ></el-input>
    </el-form-item>
    <el-form-item label="年龄" label-width="80px" prop="age" class="age">
      <el-input
        type="number"
        clearable
        v-model.number="form.age"
        autocomplete="off"
        placeholder="18"
      ></el-input>
    </el-form-item>
    <el-form-item label="性别" label-width="80px" prop="sex" class="sex">
      <label style="margin-left: 10px;">
        <el-radio @change="selectSex" name="sex" v-model="form.sex" label="男"></el-radio>
      </label>
      <label style="margin-left: 30px;">
        <el-radio @change="selectSex" name="sex" v-model="form.sex" label="女"></el-radio>
      </label>
    </el-form-item>
    <el-form-item label="出生日期" label-width="80px" prop="birth" class="birth">
      <el-date-picker
        style="width: 198px;"
        v-model="form.birth"
        type="date"
        placeholder="2000-01-01"
      ></el-date-picker>
    </el-form-item>
    <el-form-item label="地址" label-width="80px" prop="address" class="address">
      <!-- <el-cascader v-model="address" :options="options" @change="handleChange"></el-cascader> -->
      <el-input v-model.trim="form.address" clearable autocomplete="off" placeholder="北京市"></el-input>
    </el-form-item>
    <el-form-item class="longin_submit">
      <el-button
        type="success"
        style="margin-left: -50px;"
        plain
        @click="$router.replace('/login')"
        class="longin_submit"
      >登录</el-button>
      <el-button
        style="margin-left: 30px;"
        type="success"
        @click="register"
        class="longin_submit"
      >注册</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import dayjs from 'dayjs'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: "",
        password: "",
        password2: "",
        nick: '',
        age: '',
        sex: '男',
        birth: '',
        address: ''
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            message: "用户名长度不能小于3位",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        password2: [{ required: true, message: "请输入确认密码", trigger: "blur" },
        { validator: validatePass2, trigger: 'blur' }],
        nick: [{ required: true, message: "请输入昵称", trigger: "blur" }],
      },
    };
  },
  methods: {
    register() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        console.log('执行注册');

        let data = { ...this.form }
        data.age = data.age || 18
        data.birth = data.birth || '2000-01-01'
        data.birth = dayjs(data.birth).format('YYYY-MM-DD')
        data.address = data.address || '北京市'
        const res = await this.$http.post('/user.php', {
          _type: 'register',
          ...data
        })
        console.log(res)
      });
    },
    selectSex(sex) {
      this.sex = sex
    },
  },
  async mounted() {
  },
};
</script>

<style scoped>
.login-container {
  border-radius: 15px; /*圆角*/
  background-clip: padding-box; /*背景被裁剪到内边距框*/
  width: 350px; /*表单宽度*/
  height: 670px;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
  padding: 35px 35px 15px 35px; /*填充*/
  background: #fff;
  border: 1px solid #eaeaea; /*边框*/
  box-shadow: 0 0 25px #cac6c6; /*隐形*/
}

.login_title {
  margin: 0px auto 40px auto; /*上边界0 下边边界40 左右居中*/
  text-align: center; /*上面仅是标签居中 这里是文字居中*/
  color: #505458;
}

.longin_submit {
  margin: 10px auto 0px auto; /*上边界10 下边边界40 左右居中*/
}
</style>
